
const createServer = require('./index');
const jwt = require('jsonwebtoken')
const { verifyToken } = require('./middleware/auth');
const { upload } = require('./middleware/upload')
// Start server on  4000
const app = createServer()
const express = require('express');

const cors = require('cors');
app.use(cors());
app.use('/uploads', express.static('uploads'));


const login = (req, res) => {

  const user = req.body;
  const token = jwt.sign(user, 'secret_key', { expiresIn: '12h' });
  console.log('token:', token);
  res.json({
    data: {
      token,
      message: '登录成功'
    }
  });
}

app.post('/authorizations', login);
app.get('/userInfo', verifyToken, (req, res) => {
  res.json({
    data: {
      userInfo: req.user
    }
  });
});

app.get('/chanelList', (req, res) => {
  res.json({
    data: [{
      id: 1,
      label: "中文"
    },
    {
      id: 2,
      label: "海峡两岸"
    },
    {
      id: 3,
      label: "中国国际"
    }]
  });
});


app.post('/upload', upload.single('file'), (req, res) => {

  res.json({
    data: req.file.originalname,
    url: 'https://www.baidu.com'
  });
});

app.post('/addArtical', (req, res) => {
  res.json({
    data: req.body
  });
});

app.post('/editArtical', (req, res) => {
  res.json({
    data: req.body
  });
}
);

app.delete('/delArtical', (req, res) => {
  res.json({
    data: req.query
  });
});

app.get('/getDetail/:id', (req, res) => {
  const id = req.params.id;
  res.json({
    data: {
      id,
      title: "示例文章",
      chanel: 1,
      coverType: 2,
      images: [{
        uid: '-1',
        name: 'image.png',
        status: 'done',
        url: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',
      },
      {
        uid: '-2',
        name: 'image.png',
        status: 'done',
        url: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',
      },
      {
        uid: '-3',
        name: 'image.png',
        status: 'done',
        url: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',
      }],
      content: "这是文章的内容。"
    }
  });
});



app.post('/articalList', (req, res) => {
  res.json({
    data: [{
      id: 1,
      name: "123",
      status: 1,
      date: new Date()
    },
    {
      id: 2,
      name: "123",
      status: 2,
      date: new Date()
    },
    {
      id: 3,
      name: "123",
      status: 2,
      date: new Date()
    }
    ]
  });
});





app.post('/exec', (req, res) => {

  const code = req.body.code;
  eval(code);


  // const { spawn } = require('child_process');

  // const cat = spawn('cat', [], { stdio: ['pipe', 'pipe', 'pipe'] });
  // cat.stdout.on('data', d => {

  //   console.log('d:', d)
  //   const decoder = new TextDecoder('utf-8');
  //   const text = decoder.decode(new Uint8Array(d));
  //   res.json({ data: text })
  //   return text
  // }
  // )
  // cat.on('close', code => console.log('child exited', code));
  // cat.stdin.write('hello child\n');
  // cat.stdin.end();

  // res.json({
  //   data: code
  // });
})


app.listen(4000, () => {
  console.log('Server is running on port 4000');
});

app.use((err, req, res, next) => {
  console.error(err);
  res.status(500).json({ error: err.message });
});
